package com.ddm.controller.admin;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ddm.constant.MessageConstant;
import com.ddm.dto.StandardDTO;
import com.ddm.dto.TermDTO;
import com.ddm.dto.page.TermPageQueryDTO;
import com.ddm.entity.Standard;
import com.ddm.entity.Term;
import com.ddm.exception.DataErrorException;
import com.ddm.result.PageResult;
import com.ddm.result.Result;
import com.ddm.service.CommonService;
import com.ddm.service.TermService;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.ddm.constant.MessageConstant.NULL_DATA;

/**
 * <p>
 * 术语表 前端控制器
 * </p>
 *
 * @author 丁丁猫
 * @since 2025-01-13
 */
@RestController("AdminTermController")
@RequestMapping("/admin/term")
@Slf4j
public class TermController {
    @Autowired
    private CommonService commonService;

    @Autowired
    private TermService termService;

    /**
     * 新增术语
     * @param termDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增术语")
    public Result<String> add(@RequestBody TermDTO termDTO) {
        if (termDTO.getStandardId() == null) {
            throw new DataErrorException(NULL_DATA);
        }
        log.info("新增术语：{},标准表id：{}", termDTO.getCName(), termDTO.getStandardId());
        Term term = new Term();
        BeanUtils.copyProperties(termDTO, term);
        // 图片列表转字符串
        if (CollectionUtils.isNotEmpty(termDTO.getImageList())) {
            String image = commonService.convertListToString(termDTO.getImageList());
            term.setImage(image);
        }
        // 保存
        termService.save(term);
        return Result.success();
    }


    /**
     * 根据id修改术语
     * @param termDTO
     * @return
     */
    @PutMapping
    @ApiOperation("根据id修改术语")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    public Result<String> updateById(@RequestBody TermDTO termDTO) {
        log.info("根据id修改术语:{}", termDTO.getId());
        // 判断是否存在
        if (null != termService.getById(termDTO.getId())) {
            Term term = new Term();
            BeanUtils.copyProperties(termDTO, term);
            // 图片列表转字符串
            if (!termDTO.getImageList().isEmpty()) {
                String image = commonService.convertListToString(termDTO.getImageList());
                term.setImage(image);
            } else {
                term.setImage("");
            }
            // 更新
            termService.updateById(term);
            return Result.success();
        } else {
            throw new DataErrorException(MessageConstant.NULL_RECORD);
        }
    }



    /**
     * 根据关键词分页查询术语
     * @param termPageQueryDTO
     * @return
     */
    @GetMapping
    @ApiOperation("根据关键词分页查询术语")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    public Result<PageResult> pageList(TermPageQueryDTO termPageQueryDTO) {
        log.info("根据关键词分页查询术语");
        return Result.success(termService.pageList(termPageQueryDTO));
    }
    /**
     * 根据id查询术语
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询术语")
    public Result<Term> getById(@PathVariable Long id) {
        log.info("根据id查询术语:{}",id);
        return Result.success(termService.getById(id));
    }
    /**
     * 根据标准id查询术语
     * @param id
     * @return
     */
    @GetMapping("/byStandardId")
    @ApiOperation("根据标准id查询术语")
    public Result<List<Term>> getByStandardId(@RequestParam("id") Long id) {
        log.info("根据标准id查询术语:{}",id);
        return Result.success(termService.getByStandardId(id));
    }

    /**
     * 根据id删除术语
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除术语")
    public Result<String> deleteById(@PathVariable Long id) {
        log.info("根据id删除术语:{}", id);
        termService.removeById(id);
        return Result.success();
    }
}

